package com.lams.service.archive.impl;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import com.lams.jdbc.BaseDAO;
import com.lams.jdbc.IBaseDAO;
import com.lams.jdbc.QueryUtils;
import com.lams.service.BaseFacade;
import com.lams.service.archive.IAssetFacade;
import com.lams.util.IdFactory;
import com.lams.util.ResultEntity;


public class AssetFacade extends BaseFacade implements IAssetFacade{
	private String queryAssets;
	private String insertAsset;
	private String updateAsset;
	private final static String TABNAME = "c_assets";
	private final static String COLUMN_ID = "asset_id";
//	private IBaseDAO dao;
//	
//	public IBaseDAO getDao() {
//		return dao;
//	}
//	public void setDao(IBaseDAO dao) {
//		this.dao = dao;
//	}
	@Override
	public ResultEntity queryAssets(int start, int limit, Map params) {
		String sql = QueryUtils.getSqlBySqlCode(queryAssets, params);
		List<Map> data = QueryUtils.executeQuery(start, limit, sql);
		ResultEntity result = new ResultEntity();
		result.setData(data);
		result.setTotalCount(QueryUtils.getTotalCount(sql));
		result.setSuccess(true);
		result.setLimit(limit);
		result.setStart(start);
		result.setMessage("查询成功");
		return result;
	}
	public ResultEntity addOrUpdateAsset(Map params){
		ResultEntity result = new ResultEntity();
		boolean flag = false;
		if(params.get("ASSET_ID")==null || "".equals(params.get("ASSET_ID")) || "-1".equals(params.get("ASSET_ID"))){
			flag = addAsset(params);
			
		}else{
			flag = updateAsset(params);
		}
		result.setSuccess(flag);
		result.setMessage(flag?"操作成功":"操作失败");
		return result;
	}
	private boolean addAsset(Map params){
		params.put("ASSET_ID", IdFactory.getNextNav(TABNAME, COLUMN_ID));
		String sql = QueryUtils.getSqlBySqlCode(insertAsset, params);
		try {
			return super.getDao().executeUpdate(sql)<0?false:true;
		} catch (SQLException e) {
			e.printStackTrace();
		} 
		return false;	
	}
	private boolean updateAsset(Map params){
		String sql = QueryUtils.getSqlBySqlCode(updateAsset, params);
		int flag = -1;
		try {
			flag = super.getDao().executeUpdate(sql);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return flag<0?false:true;
	}
	public String getQueryAssets() {
		return queryAssets;
	}
	public void setQueryAssets(String queryAssets) {
		this.queryAssets = queryAssets;
	}
	public String getInsertAsset() {
		return insertAsset;
	}
	public void setInsertAsset(String insertAsset) {
		this.insertAsset = insertAsset;
	}
	public String getUpdateAsset() {
		return updateAsset;
	}
	public void setUpdateAsset(String updateAsset) {
		this.updateAsset = updateAsset;
	}
	
}
